﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SERIX.Classes;
using System.Data;
using System.Text;

namespace SERIX
{
    public partial class Execucao : Seguranca
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.OnLoad(e);            
            if (!IsPostBack)
                anoExec.Text = "Ano da execução: " + CPlanejamento.AnoExecucaoCorrente(usuAut.Login).ToString();
        }

        protected void obsBenef_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["login"] = this.UsuAut.Login;
        }

        protected void tbxPesquisaBenef_TextChanged(object sender, EventArgs e)
        {
            //ddlBenef.Items.Clear();
            //ddlPropriedades.Items.Clear();
            //ddlBenef.DataBind();
        }

        private string ObterIdBenef()
        {
            if (ddlBenef.SelectedValue != null && ddlBenef.SelectedValue.Contains("Benef"))
                return ddlBenef.SelectedValue.Replace("Benef", "");
            return null;
        }

        private string ObterIdOrg()
        {
            if (ddlBenef.SelectedValue != null && ddlBenef.SelectedValue.Contains("Org"))
                return ddlBenef.SelectedValue.Replace("Org", "");
            return null;
        }

        protected void ddlBenef_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlBenef.SelectedValue.Contains("Benef"))//Beneficiario
            {
                string idt_bebef = ddlBenef.SelectedValue.Replace("Benef", "");
                ddlPropriedades.DataBind();
            }
            else
                ddlPropriedades.Enabled = false;
            gridView.DataBind();
        }

        protected void odsPropriedades_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            string idt_bebef = ObterIdBenef();
            e.InputParameters["idt_benef"] = idt_bebef;
            if (idt_bebef == null)
            {
                e.InputParameters["idt_benef"] = "-1";
                ddlPropriedades.Enabled = false;
            }
            e.InputParameters["login"] = UsuAut.Login;
        }

        protected void ddlBenef_DataBound(object sender, EventArgs e)
        {
            ddlPropriedades.DataBind();
            gridView.DataBind();
        }

        protected void btnConfirmar_Click(object sender, EventArgs e)
        {
            List<StructExecucao> execucoes = new List<StructExecucao>();
            List<string> idt_Ids = new List<string>();
            foreach (GridViewRow row in gridView.Rows)
            {

                if (gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med2"] != null)
                {
                    StructExecucao chave = new StructExecucao();
                    chave.Idt_AIS = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_AIS"]);
                    chave.idt_Ind = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["idt_ind"]);
                    TextBox tbx = (TextBox)row.Cells[1].FindControl("Valor2");//Primeira Ordem
                    if (!string.IsNullOrEmpty(tbx.Text))
                    {
                        chave.Idt_Tpo_Med2 = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med2"]);
                        chave.valor2 = Convert.ToDecimal(tbx.Text);
                        tbx = (TextBox)row.Cells[2].FindControl("Valor3");//Segunda Ordem
                        if (!string.IsNullOrEmpty(tbx.Text))
                        {
                            chave.Idt_Tpo_Med3 = Convert.ToInt32(gridView.DataKeys[row.RowIndex]["Idt_Tpo_Med3"]);
                            chave.valor3 = Convert.ToDecimal(tbx.Text);
                        }
                    }
                    execucoes.Add(chave);
                }                
            }
            string idt_Tec = CTecnicos.PesquisaporCodUsu(usuAut.Codigo, usuAut.Login).Idt_tec;
            CExecucao.GravarExecucao(execucoes, idt_Tec, ObterIdBenef(), ObterIdOrg(),
                ddlMes.SelectedValue, ddlPropriedades.SelectedValue, usuAut.Login);
            lblmsg.Text = "Operação realizada com Sucesso";
            lblmsg.Visible = true;
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView row = (DataRowView)e.Row.DataItem;                
                if (row["Idt_TPo_MEd2"] != DBNull.Value)
                    ConfiguraMedida(e.Row.Cells[1], "2", row["Idt_TPo_MEd2"].ToString(), row["valor_Exec2"].ToString());
                else
                {
                    e.Row.Cells[1].Enabled = false;
                    //((RequiredFieldValidator)e.Row.Cells[2].Controls[1]).Enabled = false;
                }
                if (row["Idt_TPo_MEd3"] != DBNull.Value)
                    ConfiguraMedida(e.Row.Cells[2], "3", row["Idt_TPo_MEd3"].ToString(), row["valor_Exec3"].ToString());
                else
                {
                    e.Row.Cells[2].Enabled = false;
                    //((RequiredFieldValidator)e.Row.Cells[3].Controls[1]).Enabled = false;
                }
            }
        }

        private void ConfiguraMedida(TableCell cell, string ordem, string Idt_Tpo_Med, string valor_Prog)
        {
            DataTable dte = CProgramacao.ObterMedida(Idt_Tpo_Med, usuAut.Login);
            if (dte.Rows.Count > 0)
            {
                ((Label)cell.FindControl("Medida" + ordem)).Text = dte.Rows[0]["dsc_tpo_Med"].ToString();
                ((TextBox)cell.FindControl("Valor" + ordem)).Text = valor_Prog;
            }
        }

        protected void odsIndicadores_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {

        }

        protected void cbxProgramadoHeader_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            bool check = false;
            if (cbx.Checked)
                check = true;
            foreach (GridViewRow row in gridView.Rows)
            {
                ((CheckBox)row.FindControl("cbxProgramado")).Checked = check;
            }
        }

        protected void odsIndicadores_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            string idt_Tec = CTecnicos.PesquisaporCodUsu(usuAut.Codigo, usuAut.Login).Idt_tec;
            e.InputParameters["idt_prog"] = ddlPrograma.SelectedValue;
            e.InputParameters["idt_tec"] = idt_Tec;
            e.InputParameters["login"] = usuAut.Login;
            e.InputParameters["ano_exec"] = CPlanejamento.AnoExecucaoCorrente(usuAut.Login).ToString();
            e.InputParameters["idt_benef"] = ObterIdBenef();
            e.InputParameters["idt_org"] = ObterIdOrg();
        }

        protected void gridView_DataBound(object sender, EventArgs e)
        {
            lblmsg.Visible = false;
        }

        protected void odsAcao_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //e.InputParameters["idt_prog"] = ddlPrograma.SelectedValue;
            e.InputParameters["ano_ais"] = CPlanejamento.AnoExecucaoCorrente(usuAut.Login).ToString();
            e.InputParameters["login"] = UsuAut.Login;
        }

        protected void ddlAcao_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblmsg.Visible = false;
            gridView.DataBind();
        }

        protected void odsPrograma_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["ano_ais"] = CPlanejamento.AnoExecucaoCorrente(usuAut.Login).ToString();
            e.InputParameters["login"] = usuAut.Login;
        }

        protected void ddlPrograma_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlAcao.DataBind();
            gridView.DataBind();
        }

        protected void ddlPropriedades_DataBound(object sender, EventArgs e)
        {
            ddlPrograma.DataBind();
        }

        protected void ddlPrograma_DataBound(object sender, EventArgs e)
        {
            ddlAcao.DataBind();
        }
    }
}
